/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Admin
 */
public class DataUtil {

    private static Connection conn;
    private static String data = "";
    private static String userName = "";
    private static String password = "";
    private static boolean rsl = false;
    
    //read file and retrieve the database connection string
    public static boolean readFileConfig() {
        String str = "ConfigurationDatabase/config.properties";
        FileReader file;
        BufferedReader reader;
        String record = null;
        try {
            file = new FileReader(str);
            reader = new BufferedReader(file);
            int count = 1;
            while ((record = reader.readLine()) != null) {
                if (count == 1) {
                    data = record;
                } else if (count == 2) {
                    userName = record;
                } else if (count == 3) {
                    password = record;
                }
                count++;
            }
            if (data != null || userName != null || password != null) {
                rsl = true;
            }
            reader.close();
            file.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return rsl;
    }

    //Function connect database

    public static Connection connect() {
        try {
            boolean rs = readFileConfig();
            if (rs == true) {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                conn = java.sql.DriverManager.getConnection(data,userName,password);
            } else {
                System.out.print("Can not Connect Database. Please, check to string connection");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DataUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(DataUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return conn;

    }
    
    //function close connect
    public static void closeConnect()
    {
        try {
            connect().close();
        } catch (SQLException ex) {
            Logger.getLogger(DataUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //function Check connect
    public static Connection getconnect() {
        if (conn == null) {
            connect();
        }
        return conn;
    }

    //function query execution
    public static ResultSet executeQuery(String sql) {
        ResultSet rs = null;
        if (conn == null) {
            connect();
        } else {
            try {
                Statement st = (Statement) conn.createStatement();
                rs = st.executeQuery(sql);
            } catch (SQLException ex) {
                System.out.print(ex.getMessage());
                rs = null;
            }

        }
        return rs;
    }
    //method security 

    public static void execute(String sql) {
        try {
            Statement st = conn.createStatement();
            st.execute(sql);
        } catch (SQLException ex) {
            System.out.print(ex.getMessage());
        }
    }

    // function update execution
    public static int executeNonQuery(String sql) {
        if (conn == null) {
            connect();
        } else {
            try {
                Statement st = conn.createStatement();
                st.executeUpdate(sql);
            } catch (SQLException ex) {
                Logger.getLogger(DataUtil.class.getName()).log(Level.SEVERE, null, ex);
                return 0;
            }
        }
        return 1;
    }
}
